--慑敌威光
local shediweiguang = fk.CreateSkill {
    name = "mcdh_shediweiguang",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

shediweiguang:addEffect('viewas',{
    anim_type = "offensive",
    card_filter = Util.FalseFunc,
    view_as = function (self, player,cards)
        local c = Fk:cloneCard("slash")
        c.skillName = self.name
        return c
    end,
    before_use = function (self, player, use)
        lib.LoseCharge(player,player,3,self.name)
        use.extraUse = true
        use.extra_data = use.extra_data or {}
        use.extra_data.mcdh_shediweiguang = player
    end,
    enabled_at_play = function(self, player)
        return player:getMark("mcdh_charge") >= 3
    end,
    enabled_at_response = Util.FalseFunc,
})

shediweiguang:addEffect(fk.CardUseFinished,{
    can_refresh = function (self, event, target, player, data)
        return data.extra_data and data.extra_data.mcdh_shediweiguang  == player
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
            if not data.damageDealt then
                local tos = room:askToChoosePlayers(player, {
                    targets = table.filter(room:getAlivePlayers(), function(p)
                        return p:isWounded()
                    end),
                    min_num = 1,
                    max_num = 1,
                    prompt = "#mcdh_shediweiguang_recover",
                    skill_name = self.name,
                    cancelable = true
                  })
                if #tos > 0 then
                    room:recover({
                        who = tos[1],
                        num = 1,
                        recoverBy = player,
                        skillName = self.name
                    })
                end
            end
    end
})

shediweiguang:addEffect(fk.Damage,{
    can_refresh = function (self, event, target, player, data)
        local room = player.room
        local logic = room.logic
        local damageEvent = logic:getCurrentEvent()
        local useEvent =  damageEvent:findParent(GameEvent.UseCard)
        if data.card.trueName == "slash" and useEvent and useEvent.data.extra_data and useEvent.data.extra_data.shediweiguang then
            event:setCostData(self,useEvent.data.extra_data.shediweiguang)
            return data.to == player
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local use = event:getCostData(self)
        local mark = player:getTableMark("@@mcdh_shediweiguang")
        table.insertIfNeed(mark,use.id)
        room:setPlayerMark(player,"@@mcdh_shediweiguang",mark)
    end
})

shediweiguang:addEffect(fk.TurnStart,{
    can_refresh = function (self, event, target, player, data)
        return target == player and type(target:getMark("@@mcdh_shediweiguang")) == "table"
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player,"@@mcdh_shediweiguang",0)
    end
})

shediweiguang:addEffect('targetmod',{
    bypass_times = function(self, player, skill, scope, card)
        return card and scope == Player.HistoryPhase and table.contains(card.skillNames, "mcdh_shediweiguang")
    end,
})

shediweiguang:addEffect('prohibit',{
    is_prohibited = function (self, from, to, card)
        if type(to:getMark("@@mcdh_shediweiguang")) == "table" then
            if from == to then
                return true
            elseif #to:getMark("@@mcdh_shediweiguang") == 1 then
                return to:getMark("@@mcdh_shediweiguang")[1] == from.id
            else
                    return false
            end
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_shediweiguang"] = "威光",
    [":mcdh_shediweiguang"] = "『慑敌威光』<br>蓄力技(3)，出牌阶段，你可以视为对一名其他角色使用一张无次数限制的【杀】。此【杀】造成伤害后，受到伤害的角色直到其下一个回合开始前，不能被除你和其自身以外的角色使用牌指定为目标；若此【杀】未造成伤害，则你可以令一名角色回复1点体力。",
    ["#mcdh_shediweiguang_recover"] = "慑敌威光：此【杀】未造成伤害，你可以令一名角色回复1点体力。",
}

return shediweiguang